package han.controller;

import han.pojo.Question;
import han.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @authou HanDianWei
 * @create 2020-10-28 14:51
 */
@Controller
@RequestMapping("question")
public class QuestionController {
    @Autowired
    @Qualifier("QuestionService")
    private QuestionService questionService;

//    获取全部的信息
    @RequestMapping("/allQuestion")
    public String list(Model model){
        List<Question> questionList = questionService.queryQuestion();
        model.addAttribute("list",questionList);
        return "tourists/allQuestion";
    }
//    管理员
    @RequestMapping("/allQuestionA")
    public String allQuestionA(Model model){
        List<Question> questionList = questionService.queryQuestion();
        model.addAttribute("list",questionList);
        return "admin/allQuestion";
    }

//    根据id查找
@RequestMapping("/questionDetail/{q_id}")
public String selectByPrimaryKey(@PathVariable("q_id")int id, Model model){
    Question question = questionService.selectByPrimaryKey(id);
    List<Question> questions = new ArrayList<Question>();
    questions.add(question);
    model.addAttribute("qDetail",questions);
    return "tourists/questionDetail";
}
//   添加热门问题

    @RequestMapping("/toAddQuestion")
    public String toAddQuestion(){
        return "admin/addQuestion";
    }

    @RequestMapping("/addQuestion")
    public String addQuestion(Question question){
        questionService.insert(question);
        return "redirect:allQuestionA";
    }
    @RequestMapping("/toUpdate")
    public String toUpdateQuestion(int id,Model model){
        Question question = questionService.selectByPrimaryKey(id);
        model.addAttribute("list",question);
        return "admin/updQuestion";
    }

    @RequestMapping("/updateQuestion")
    public String updateQuestion(Question question){
        questionService.updateByPrimaryKey(question);
        return "redirect:allQuestionA";
    }

    @RequestMapping("/delQuestion")
    public String delQuestion (int q_id){
        questionService.deleteByPrimaryKey(q_id);
        return "redirect:allQuestionA";
    }

}
